package com.zh.edu.info.manager.controller;

import com.zh.edu.info.manager.domain.Student;
import com.zh.edu.info.manager.service.StudentService;

import java.util.Scanner;

public abstract class BaseStudentController {
    private Scanner sc = new Scanner(System.in);
    private StudentService studentService = new StudentService();
    public void start() {
        sl:
        while (true) {
            System.out.println("--------欢迎来到zh<学生>信息管理系统--------");
            System.out.println("1.添加学生   2.删除学生   3.修改学生   4.查询学生   5.退出");
            System.out.print("请输入您的选择：");
            String choice = sc.next();
            switch (choice) {
                case "1":
                    addStudent();
                    break ;
                case "2":
                    selectStudent();
                    break;
                case "3":
                    setStudent();
                    break;
                case "4":
                    showStudent();
                    break;
                case "5":
                    System.out.println("感谢使用学生管理系统，再见");
                    break sl;
                default:
                    System.out.println("您的输入有误，请重新输入");
                    break;

            }
        }
    }

    private void setStudent() {
        String id = inputId();
        Student student = inputStudent(id);
        studentService.setStudent(id, student);
        System.out.println("修改成功");

    }

    private void selectStudent() {
        String id = inputId();
        studentService.selectStudent(id);
        System.out.println("删除成功");
    }

    private void showStudent() {
        Student[] arr = studentService.showStudent();
        if (arr == null) {
            System.out.println("查无信息，请添加后重试");
        }else {
            System.out.println("学号\t\t姓名\t\t年龄\t\t生日");
            for(int i = 0; i < arr.length; i++) {
                if(arr[i] != null) {
                    System.out.println(arr[i].getId()+"\t\t"+arr[i].getName()+"\t\t"+arr[i].getAge()+"\t\t"+arr[i].getBirthday());
                }
            }
        }

    }

    public void addStudent() {
        String id;
        while (true) {
            System.out.println("请输入学生id：");
            id = sc.next();
            boolean flag = studentService.idFind(id);
            if (flag) {
                System.out.println("学号已被占用，请重新输入");
            } else {
                break;
            }
        }
        Student student = inputStudent(id);
        boolean result = studentService.addStudent(student);
        if (result) {
            System.out.println("添加成功");
        } else {
            System.out.println("添加失败");
        }
    }

    abstract Student inputStudent(String id);

    private String inputId() {
        System.out.println("请输入学生id:");
        String id;
        while (true) {
            id= sc.next();
            boolean flag = studentService.idFind(id);
            if (flag) {
                break;
            } else {
                System.out.println("学号不存在，请重新输入:");
            }
        }
        return id;
    }

}
